package cn.px.service.upms.impl;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.enums.ResultCode;
import cn.px.common.exception.BusinessException;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.IdGeneratorUtils;
import cn.px.dao.dto.upms.SysTeamDTO;
import cn.px.dao.dto.upms.SysUserDTO;
import cn.px.dao.entity.upms.SysTeam;
import cn.px.dao.mapper.upms.SysTeamMapper;
import cn.px.dao.vo.SysUserVO;
import cn.px.service.base.impl.BaseServiceImpl;
import cn.px.service.upms.SysTeamService;
import cn.px.service.upms.SysUserService;
import cn.px.service.upms.SysUserTeamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.List;

/**
 * @Description 群组 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@AllArgsConstructor
@Service("sysTeamService")
public class SysTeamServiceImpl extends BaseServiceImpl<SysTeamMapper, SysTeam, SysTeamDTO> implements SysTeamService {

    private final SysUserTeamService sysUserTeamService;

    private final SysUserService sysUserService;

    @Override
    public int batchDelete(List<String> ids) {
        Weekend<SysTeam> weekend = new Weekend<>(SysTeam.class);
        WeekendCriteria<SysTeam, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andIn(SysTeam::getId, ids);
        mapper.deleteByExample(weekend);
        return 1;
    }

    @Override
    public SysTeam saveModelByDTO(SysTeamDTO model) {
        setCreateUserInfo(model);
        SysTeam entity = BeanConverterUtil.copyObjectProperties(model, SysTeam.class);
        String teamId = IdGeneratorUtils.getObjectId();
        entity.setId(teamId);
        sysUserTeamService.saveUserTeams(teamId, model.getUserIds());
        mapper.insertSelective(entity);
        return entity;
    }

    @Override
    public void modModelByDTO(SysTeamDTO model) {
        setUpdateUserInfo(model);
        SysTeam entity = BeanConverterUtil.copyObjectProperties(model, SysTeam.class);
        String teamId = entity.getId();
        sysUserTeamService.deleteByTeamId(teamId);
        sysUserTeamService.saveUserTeams(teamId, model.getUserIds());
        mapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public SysTeam getModelById(Object id) {
        SysTeam team = super.getModelById(id);
        if (team == null) {
            throw new BusinessException(ResultCode.DATA_NOT_EXIST);
        }
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setTeamId((String) id);
        List<SysUserVO> userVOS = sysUserService.selectUserVOs(userDTO);
        team.setUsers(userVOS);
        return team;
    }

    @Override
    public AbstractPageResultBean<SysTeam> selectPageInfo(SysTeamDTO record, int pageNum, int pageSize) {
        AbstractPageResultBean<SysTeam> page = super.selectPageInfo(record, pageNum, pageSize);
        return page;
    }
}
